/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package edu.upn.sigemfi.vfi.bc;

import edu.upn.sigemfi.ope.be.Moneda;
import edu.upn.sigemfi.vfi.be.Caja;
import edu.upn.sigemfi.vfi.be.SaldoCaja;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

/**
 *
 * @author Programador
 */
public class SaldoCajaDelegate {

    private SaldoCajaFacadeLocal local;

    public SaldoCajaDelegate() {
        local = lookupSaldoCajaFacade();
    }

    public void crear(SaldoCaja saldoCaja) throws Exception {
        try {
            local.create(saldoCaja);
        } catch (Exception e) {
            local = lookupSaldoCajaFacade();
            throw new Exception("Crear SaldoCaja. " + e.getMessage(), e);
        }
    }

    public void editar(SaldoCaja saldoCaja) throws Exception {
        try {
            local.edit(saldoCaja);
        } catch (Exception e) {
            local = lookupSaldoCajaFacade();
            throw new Exception("Editar SaldoCaja. " + e.getMessage(), e);
        }
    }

    public void eliminar(SaldoCaja saldoCaja) throws Exception {
        try {
            local.remove(saldoCaja);
        } catch (Exception e) {
            local = lookupSaldoCajaFacade();
            throw new Exception("Eliminar SaldoCaja. " + e.getMessage(), e);
        }
    }

    public SaldoCaja buscar(Long codigo) throws Exception {
        try {
            return local.find(codigo);
        } catch (Exception e) {
            local = lookupSaldoCajaFacade();
            throw new Exception("Buscar SaldoCaja. " + e.getMessage(), e);
        }
    }


     public SaldoCaja buscarPorCajayMoneda(Caja fidVfiCaja, Moneda fidOpeMoneda) throws Exception {
        try {
            return local.buscarPorCajayMoneda(fidVfiCaja, fidOpeMoneda);
        } catch (Exception e) {
            local = lookupSaldoCajaFacade();
            throw new Exception("Buscar SaldoCaja. " + e.getMessage(), e);
        }
    }

     public SaldoCaja buscarMoneda(Long codigo, Moneda m) throws Exception {
        try {
            return local.findbyMoneda(codigo,m);
        } catch (Exception e) {
            local = lookupSaldoCajaFacade();
            throw new Exception("Buscar SaldoCaja. " + e.getMessage(), e);
        }
    }

    public List<SaldoCaja> listar() throws Exception {
        try {
            return local.findAll();
        } catch (Exception e) {
            local = lookupSaldoCajaFacade();
            throw new Exception("Listar SaldoCaja. " + e.getMessage(), e);
        }
    }

    private SaldoCajaFacadeLocal lookupSaldoCajaFacade() {
        try {
            Context c = new InitialContext();
            return (SaldoCajaFacadeLocal) c.lookup("java:comp/env/SaldoCajaFacade");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }





}
